package com.googlecode.androidutils.app;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

/**
 * Utility class that provides convenient methods for asking the user for
 * confirmation.
 * <p>
 * This class uses a simplified callback interface for approval clicks:
 * {@link OnConfirmationCallback}. If you need to know all the details
 * {@link OnClickListener} may provide or if you are interested in clicks on
 * "Cancel" or alike buttons, use the {@link AlertDialog} and its
 * {@link Builder} directly.
 * </p>
 * <p>
 * Example usage:
 * 
 * <pre>
 * ConfirmationHelper.showYesNoInfoConfirmation(this,
 *     R.string. ..., R.string. ..., new OnConfirmationCallback() {
 *       &#064;Override
 *       public void onConfirmation() {
 *         // ...
 *       }
 *     });
 * </pre>
 * 
 * </p>
 * 
 * @see AlertDialog
 * @see AlertDialog.Builder
 * 
 * @author Alexander Metzner
 * 
 */
public final class ConfirmationHelper {

  /** Utility class; use static methods. */
  private ConfirmationHelper() {
    // Use static methods
  }

  public static void showYesNoInfoConfirmation(final Context context,
      final int messageId, final int titleId,
      final OnConfirmationCallback callback) {
    showYesNoConfirmation(context, messageId, titleId,
        android.R.drawable.ic_dialog_info, callback);
  }

  public static void showYesNoAlertConfirmation(final Context context,
      final int messageId, final int titleId,
      final OnConfirmationCallback callback) {
    showYesNoConfirmation(context, messageId, titleId,
        android.R.drawable.ic_dialog_alert, callback);
  }

  public static void showYesNoConfirmation(final Context context,
      final int messageId, final int titleId, final int iconId,
      final OnConfirmationCallback callback) {
    showConfirmation(context, messageId, titleId, iconId, android.R.string.yes,
        android.R.string.no, callback);
  }

  public static void showOkCancelInfoConfirmation(final Context context,
      final int messageId, final int titleId,
      final OnConfirmationCallback callback) {
    showOkCancelConfirmation(context, messageId, titleId,
        android.R.drawable.ic_dialog_info, callback);
  }

  public static void showOkCancelAlertConfirmation(final Context context,
      final int messageId, final int titleId,
      final OnConfirmationCallback callback) {
    showOkCancelConfirmation(context, messageId, titleId,
        android.R.drawable.ic_dialog_alert, callback);
  }

  public static void showOkCancelConfirmation(final Context context,
      final int messageId, final int titleId, final int iconId,
      final OnConfirmationCallback callback) {
    showConfirmation(context, messageId, titleId, iconId, android.R.string.ok,
        android.R.string.cancel, callback);
  }

  public static void showConfirmation(final Context context,
      final int messageId, final int titleId, final int iconId,
      final int confirmButtonId, final int dismisButtonId,
      final OnConfirmationCallback callback) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setIcon(iconId);
    builder.setTitle(titleId);
    builder.setMessage(messageId);
    builder.setPositiveButton(confirmButtonId,
        new OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            callback.onConfirmation();
          }
        });
    builder.setNegativeButton(dismisButtonId, null);
    builder.show();
  }

  public static interface OnConfirmationCallback {
    void onConfirmation();
  }

}
